package com.btshop.goods.service.impl;
import com.btshop.goods.mapper.SpecExtendMapper;
import com.btshop.goods.mapper.SpecMapper;
import com.btshop.goods.pojo.Spec;
import com.btshop.goods.pojo.SpecExample;
import com.btshop.goods.service.SpecService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import com.util.IdWorker;

/**
 * @ClassName: SpecServiceImpl
 * @description: Spec业务层接口实现类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private SpecExtendMapper specExtendMapper;


    /**
     * 根据分类id查询规格参数集合
     * @param categoryId
     * @return
     */
    @Override
    public List<Spec> findByCategoryId(Long categoryId) {
        return specExtendMapper.findSpecByCategoryId(categoryId);
    }

    /**
     * Spec条件+分页查询
     * @param spec 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Spec> findPage(Spec spec, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        SpecExample example = createExample(spec);
        //执行搜索
        return new PageInfo<Spec>(specMapper.selectByExample(example));
    }

    /**
     * Spec分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Spec> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Spec>(this.buildSelectAll());
    }

    /**
     * Spec条件查询
     * @param spec
     * @return
     */
    @Override
    public List<Spec> findList(Spec spec){
        //构建查询条件
        SpecExample example = createExample(spec);
        //根据构建的条件查询数据
        return specMapper.selectByExample(example);
    }


    /**
     * Spec构建查询对象
     * @param spec
     * @return
     */
    public SpecExample createExample(Spec spec){
        SpecExample example=new SpecExample();
        SpecExample.Criteria criteria = example.createCriteria();
        if(spec!=null){
            // ID
            if(!StringUtils.isEmpty(spec.getId())){
                    criteria.andIdEqualTo(spec.getId());
            }
            // 名称
            if(!StringUtils.isEmpty(spec.getName())){
                    criteria.andNameLike("%"+spec.getName()+"%");
            }
            // 规格选项
            if(!StringUtils.isEmpty(spec.getOptions())){
                    criteria.andOptionsEqualTo(spec.getOptions());
            }
            // 排序
            if(!StringUtils.isEmpty(spec.getSeq())){
                    criteria.andSeqEqualTo(spec.getSeq());
            }
            // 模板ID
            if(!StringUtils.isEmpty(spec.getTemplateId())){
                    criteria.andTemplateIdEqualTo(spec.getTemplateId());
            }
            // 创建时间
            if(!StringUtils.isEmpty(spec.getCreateTime())){
                    criteria.andCreateTimeEqualTo(spec.getCreateTime());
            }
            // 更新时间
            if(!StringUtils.isEmpty(spec.getUpdateTime())){
                    criteria.andUpdateTimeEqualTo(spec.getUpdateTime());
            }
            // 乐观锁版本号
            if(!StringUtils.isEmpty(spec.getOptimistic())){
                    criteria.andOptimisticEqualTo(spec.getOptimistic());
            }
            // 备注字段
            if(!StringUtils.isEmpty(spec.getRemark())){
                    criteria.andRemarkEqualTo(spec.getRemark());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delete(Long id){
        specMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Spec
     * @param spec
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void update(Spec spec){
        specMapper.updateByPrimaryKey(spec);
    }

    /**
     * 增加Spec
     * @param spec
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Spec spec){
        //设置主键值
        spec.setId(idWorker.nextId());
        specMapper.insert(spec);
    }

    /**
     * 根据ID查询Spec
     * @param id
     * @return
     */
    @Override
    public Spec findById(Long id){
        return  specMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Spec全部数据
     * @return
     */
    @Override
    public List<Spec> findAll() {
        return this.buildSelectAll();
    }

    public List<Spec> buildSelectAll(){
        return specMapper.selectByExample(null);
    }
}
